package cn.iocoder.yudao.module.alipay.controller.admin.surname;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.surname.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.surname.SurnameDO;
import cn.iocoder.yudao.module.alipay.convert.surname.SurnameConvert;
import cn.iocoder.yudao.module.alipay.service.surname.SurnameService;

@Tag(name = "管理后台 - 姓氏")
@RestController
@RequestMapping("/alipay/surname")
@Validated
public class SurnameController {

    @Resource
    private SurnameService surnameService;

    @PostMapping("/create")
    @Operation(summary = "创建姓氏")
    @PreAuthorize("@ss.hasPermission('alipay:surname:create')")
    public CommonResult<Long> createSurname(@Valid @RequestBody SurnameCreateReqVO createReqVO) {
        return success(surnameService.createSurname(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新姓氏")
    @PreAuthorize("@ss.hasPermission('alipay:surname:update')")
    public CommonResult<Boolean> updateSurname(@Valid @RequestBody SurnameUpdateReqVO updateReqVO) {
        surnameService.updateSurname(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除姓氏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:surname:delete')")
    public CommonResult<Boolean> deleteSurname(@RequestParam("id") Long id) {
        surnameService.deleteSurname(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得姓氏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:surname:query')")
    public CommonResult<SurnameRespVO> getSurname(@RequestParam("id") Long id) {
        SurnameDO surname = surnameService.getSurname(id);
        return success(SurnameConvert.INSTANCE.convert(surname));
    }

    @GetMapping("/list")
    @Operation(summary = "获得姓氏列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:surname:query')")
    public CommonResult<List<SurnameRespVO>> getSurnameList(@RequestParam("ids") Collection<Long> ids) {
        List<SurnameDO> list = surnameService.getSurnameList(ids);
        return success(SurnameConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得姓氏分页")
    @PreAuthorize("@ss.hasPermission('alipay:surname:query')")
    public CommonResult<PageResult<SurnameRespVO>> getSurnamePage(@Valid SurnamePageReqVO pageVO) {
        PageResult<SurnameDO> pageResult = surnameService.getSurnamePage(pageVO);
        return success(SurnameConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出姓氏 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:surname:export')")
    @OperateLog(type = EXPORT)
    public void exportSurnameExcel(@Valid SurnameExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SurnameDO> list = surnameService.getSurnameList(exportReqVO);
        // 导出 Excel
        List<SurnameExcelVO> datas = SurnameConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "姓氏.xls", "数据", SurnameExcelVO.class, datas);
    }

}
